#ifndef __NVM_FLASH_EEPROM__
#define __NVM_FLASH_EEPROM__

#include <stdint.h>

//=======Flash参数定义=======
#define FLASH_SECTOR_SIZE      (0x800)                // Flash擦除单位:2KB
#define FLASH_PROGRAM_SIZE     (4)                    // Flash写单位:4Byte
#define USR_FLAH_NUM_SIZE      (0X4000)               // Data Flash size = 16KB
#define USR_FLASH_BASE_ADDRESS ((uint32_t)0x0001C000) // eeprom start addr

//===========================
#define VM_VALID_FLAG         ((uint32_t)0x55AA55AA)
#define USR_FLASH_HALF_SIZE   (USR_FLAH_NUM_SIZE / 2)
#define FLASH_HALF_SECTOR_NUM (USR_FLASH_HALF_SIZE / FLASH_SECTOR_SIZE)

#define FLASH_USR_PAGE1_START (USR_FLASH_BASE_ADDRESS)
#define FALSH_USR_PAGE2_START (FLASH_USR_PAGE1_START + USR_FLASH_HALF_SIZE)

#define VM_INDEX_POINT_LEN (110)

typedef struct
{
    uint32_t id_addr;
    uint32_t data_len;
} VM_Mage_t;

typedef enum
{
    VM_INDEX_DEV_ID = 1,
    VM_INDEX_DATA_FORMAT,
    VM_INDEX_POINT0,
    VM_INDEX_POINT11 = VM_INDEX_POINT0 + 13, // 11 -> 13 becase sensor point num 12 -> 14
    VM_INDEX_COMPENSATION,
    VM_INDEX_COMP_CONFIG,
    VM_INDEX_DATA_ENCRYPT,
    VM_INDEX_MAX,
} VM_Idx_e;

typedef struct
{
    uint32_t  valid_flag;
    uint32_t  err_flag;
    uint32_t  last_addr;
    VM_Mage_t vm_mage[VM_INDEX_MAX];
} VM_HDL_t;

typedef enum
{
    VM_OVER_WORD_SIZE,
    VM_READ_DATA_OK,
    VM_READ_DATA_ERR,
    VM_EEPROM_BUSY,
    VM_ID_OVER,
    VM_INIT_ERR,
} VM_RETURN_MSG_e;

void     VME2P_Initialize(void);
uint32_t VME2P_Read(uint32_t index, uint8_t *buf, uint32_t len);
uint32_t VME2P_Write(uint32_t index, const uint8_t *buf, uint32_t len);

#endif
